local BgMap = require("app.map.BgMap")
local MouseSp = require("app.map.MouseSp")
local MapPoint = require("app.map.MapPoint")
local StageUILayer = require("app.layers.stg_StageUILayer")

local StageScene = class("StageScene", cc.load("mvc").ViewBase)

function StageScene:onCreate()
    self.mPlayerDirection = cc.p(0, 0) 
    self.mTouchMouse = nil
    
    self:init()
end

function StageScene:onEnter()
    FzEventCenter:RegisterEvent(FzEvent.SwitchMap,self,self.onSwitchMap)
end

function StageScene:onExit()
    FzEventCenter:RemoveEvent(FzEvent.SwitchMap,self,self.onSwitchMap)
    MapController:unloadBgMap()
    self:stopUpdateStageScene()
    if self.touch_lis then
        self:getEventDispatcher():removeEventListener(self.touch_lis)
        self.touch_lis=nil
    end
end

function StageScene:init()
    self.layer = display.newLayer()
    
    local lis=cc.EventListenerTouchOneByOne:create()
    lis:registerScriptHandler(handler(self,self.onTouchBegan),cc.Handler.EVENT_TOUCH_BEGAN)
    lis:registerScriptHandler(handler(self,self.onTouchMoved),cc.Handler.EVENT_TOUCH_MOVED)
    lis:registerScriptHandler(handler(self,self.onTouchEnded),cc.Handler.EVENT_TOUCH_ENDED)
    lis:registerScriptHandler(handler(self,self.onTouchCancelled),cc.Handler.EVENT_TOUCH_CANCELLED)
    self:getEventDispatcher():addEventListenerWithSceneGraphPriority(lis,self.layer)
    self.touch_lis=lis

    self:addChild(self.layer, -10000)

    self.layer:setTouchEnabled(true)
    
    self:switchMap()

    self.uiLayer = StageUILayer.new()
    self:addChild(self.uiLayer,10000)
    
    return true
end

function StageScene:switchMap()
    if self.mTouchMouse then
        self.mTouchMouse:removeFromParent()
    end
    MapController:switchMap()
    local map = MapController:getCurMap()
    self:addChild(map,-1)
    self.mTouchMouse = MouseSp.new()
    self.mTouchMouse:setPosition(-100, -100)
    map:addChild(self.mTouchMouse, BgMap.getZOrderZero(map))

    NetEngine:sendMessage(ProtoCmd.ReqEnterStageMessage,{rid=RoleData.player_info.rid},false)

    self:startUpdateStageScene()
end

function StageScene:onSwitchMap()
    --self:stopUpdateStageScene()
    MyApp:enterScene("GameLoading")
end

function StageScene:onTouchBegan(touch,event)
    print("------------------------onTouchBegan")
    local point = touch:getLocation()
    point = MapController:getCurMap():convertToNodeSpace(point)
    
    if (not self:isVisible()) then
        return false
    end

    self.mPlayerDirection = MapController:getCurMap():convertToWorldSpace(point)
    
    local point = MapController:getCurMap():convertToNodeSpace(self.mPlayerDirection)
    local mpoint = MapPoint.new(point)
    MapController:goTo(mpoint)
    self.mTouchMouse:playEffect(mpoint:getCCPointValue())

    return true
end


function StageScene:beginMoveActions()
    print("beginMoveActions")
    
end

function StageScene:onTouchMoved(touch,event)    
    
end

function StageScene:onTouchEnded(touch,event)
    
end

function StageScene:onTouchCancelled(touch,event)
end    


function StageScene:updateScene(dt)
    self.uiLayer:updateContent()
end

-- 开启更新地图计时器
function StageScene:startUpdateStageScene()
    self:stopUpdateStageScene()
    if not self.scheduleUpdateScene then
        print("startUpdateStageScene")
		self.scheduleUpdateScene = cc.Director:getInstance():getScheduler():scheduleScriptFunc(handler(self, self.updateScene), 1/10, false)
        print("startUpdateStageScene")
	end
end

-- 关闭更新地图计时器
function StageScene:stopUpdateStageScene()
    if self.scheduleUpdateScene then
		cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.scheduleUpdateScene)
		self.scheduleUpdateScene = nil
	end
end

return StageScene
